<template>
  <div>
    <div>
      <!-- 表格操作项 start  -->
      <Button v-for="(item, index) in tableBtn" :key="index" type="primary" :icon="item.icon" class="ivu-mr-8"
              @click="tableTopOperate(item.name)">{{ item.label }}
      </Button>
      <!-- <Button icon="md-list"
              class="ivu-ml-8"
              v-show="tableBatchBtns && tableBatchBtns.length>0 && tableSelectData &&tableSelectData.length">批量操作</Button> -->
      <Dropdown class="ivu-ml-8" v-show="tableSelectData && tableSelectData.length>0" @on-click="handleClickItem">
        <Button>更多操作
          <Icon type="ios-arrow-down"/>
        </Button>
        <DropdownMenu slot="list">
          <template v-for="(item, index) in tableBatchBtns">
            <DropdownItem :key="index" :name="item.name" v-if="showBtn(item.label)">{{ item.label }}
            </DropdownItem>
          </template>

        </DropdownMenu>
      </Dropdown>
      <!-- 表格操作项 end  -->
    </div>
    <Modal v-model="auditModal" title="批量审核">
      <Form ref="auditForm" :label-width="100">
        <Row>
                    <span style="margin-bottom: 10px;">
                        确定要批量审核这些品类吗？
                    </span>
        </Row>
        <Input v-model="reason" maxlength="100" show-word-limit
               type="textarea" placeholder="请输入原因"/>
      </Form>
      <div slot="footer">
        <Button type="primary" :loading="aloading" @click.stop="handleAgree('auditForm')">
          <span v-if="!aloading">审核通过</span>
          <span v-else>Loading...</span>
        </Button>
        <Button type="default" :loading="cloading" @click.stop="handleCancel('auditForm')">
          <span v-if="!cloading">审核不通过</span>
          <span v-else>Loading...</span>
        </Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import dynamictablebtns from '@/mixins/dynamictablebtns'; // 混入通用代码
import {ApiRequest, ApiRequestExportTable} from '@api/basePage';
import {mapGetters} from "vuex";

export default {
  mixins: [
    dynamictablebtns
  ],
  data() {
    return {
      auditModal: false,
      aloading: false,
      cloading: false,
      reason: '',
    }
  },
  methods: {
    tableTopOperate(name) {
      if (name === 'export') {//导出
        let searchParam = [];
        searchParam.push({name: 'categoryNo', value: ''});
        searchParam.push({name: 'categoryNo_q', value: 'EQ'});
        searchParam.push({name: 'categoryName', value: ''});
        searchParam.push({name: 'categoryName_q', value: 'Like'});
        searchParam.push({name: 'state', value: ''});
        searchParam.push({name: 'state_q', value: 'EQ'});
        let data = {
          searchParam: JSON.stringify(searchParam),
        }
        ApiRequestExportTable(this.requestApi.exportData.url, this.requestApi.exportData.type,
          data, true).then(res => {
          const blob = new Blob([res], {
            type: "application/octet-stream",
          });
          var link = document.createElement("a");
          link.href = window.URL.createObjectURL(blob);
          link.download = "后台品类表.xlsx";
          link.click();
          window.URL.revokeObjectURL(blob); // 释放掉blob对象
        }).catch((err) => {
          this.$Message.Message("导出失败");
        });
      } else {//新增
        this.$router.push({path: '/basics/category/categoryAuditManage/categoryAuditAdd', query: {}})
      }
    },
    handleClickItem(name) {
      if (name === 'audit') {
        this.auditModal = true;
      }
    },
    // 获得选中项的uuid集合
    getUuids() {
      let uuidArr = []
      this.tableSelectData.forEach(item => {
        uuidArr.push(item.uuid)
      });
      return uuidArr.join(',');
    },
    // 审核通过
    handleAgree(name) {
      this.aloading = true;
      let data = {
        uuids: this.getUuids(),
      }
      // 开始请求商品列表
      ApiRequest(this.requestApi.auditPass.url + '?uuids=' + this.getUuids() + '&auditNote=' + this.reason,
        this.requestApi.auditPass.type, data).then(res => {
        this.aloading = false;
        this.$Message.success('批量审核成功');
        this.auditModal = false;
        this.tableSelectData = [];
        this.actionsChange();
      }).catch((err) => {
        this.aloading = false;
      });
    },
    // 拒绝审核
    handleCancel(name) {
      if (this.reason === '') {
        this.$Message.info('审核原因不能为空!');
        return;
      }
      this.aloading = true;
      let data = {
        uuids: this.getUuids(),
      }
      // 开始请求商品列表
      ApiRequest(this.requestApi.auditNoPass.url + '?uuids=' + this.getUuids() + '&auditNote=' + this.reason,
        this.requestApi.auditNoPass.type, data).then(res => {
        this.aloading = false;
        this.$Message.success('批量拒绝成功');
        this.auditModal = false;
        this.tableSelectData = [];
        this.actionsChange();
      }).catch((err) => {
        this.aloading = false;
      });
    }
  },
  computed: {
    ...mapGetters('permissions/systemBtns', [
      'showBtn'
    ]),
  },
}
</script>
